package com.example.itemdecorationdemo.fragment

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.example.itemdecorationdemo.R
import com.example.itemdecorationdemo.fragment.SimpleDividerFragment.Companion.callback
import com.example.itemdecorationdemo.model.MainViewModel
import com.example.itemdecorationdemo.itemDecoration.SectionDecoration
import com.example.itemdecorationdemo.itemDecoration.SectionDecoration.DecorationCallback

class SectionFragment : Fragment() {
    private var mViewModel: MainViewModel? = null
    private lateinit var recyclerView: RecyclerView
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.section_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mViewModel = ViewModelProvider(this).get(MainViewModel::class.java)
        // TODO: Use the ViewModel
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        recyclerView = view.findViewById(R.id.section_fragment_rv)
        val layoutManager = LinearLayoutManager(context)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerView.setLayoutManager(layoutManager)
        //recyclerView.addItemDecoration(new SimpleDividerDecoration());

        //recyclerView.addItemDecoration(new DividerItemDecoration(getContext()));
        //recyclerView.addItemDecoration(new LeftAndRightTagDecoration(getContext()));
        recyclerView.addItemDecoration(SectionDecoration(context, callback))
        recyclerView.adapter = object : RecyclerView.Adapter<ViewHolder>() {
            override fun onCreateViewHolder(
                parent: ViewGroup,
                viewType: Int
            ): ViewHolder {
                val view = LayoutInflater.from(parent.context)
                    .inflate(R.layout.main_fragment_item, parent, false)
                return ViewHolder(view)
            }

            override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                holder.title.text = "第" + position + "个item"
            }

            override fun getItemCount(): Int {
                return 100
            }
        }
    }

    //简单分割线
    class SimpleDividerDecoration : ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            outRect[20, 40, 5] = 10
        }
    }

    class DividerItemDecoration(context: Context) : ItemDecoration() {
        private val dividerHeight: Int
        private val dividerPaint: Paint
        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            super.getItemOffsets(outRect, view, parent, state)
            outRect.bottom = dividerHeight
        }

        override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
            val childCount = parent.childCount
            val left = parent.paddingLeft
            val right = parent.width - parent.paddingRight
            for (i in 0 until childCount - 1) {
                val view = parent.getChildAt(i)
                val top = view.bottom.toFloat()
                val bottom = (view.bottom + dividerHeight).toFloat()
                c.drawRect(left.toFloat(), top, right.toFloat(), bottom, dividerPaint)
            }
        }

        init {
            dividerPaint = Paint()
            dividerPaint.color = context.resources.getColor(R.color.purple_500)
            dividerHeight = context.resources.getDimensionPixelSize(R.dimen.divider_height)
        }
    }

    inner class LeftAndRightTagDecoration(context: Context) : ItemDecoration() {
        private val tagWidth: Int
        private val leftPaint: Paint
        private val rightPaint: Paint
        override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
            super.onDrawOver(c, parent, state)
            val childCount = parent.childCount
            for (i in 0 until childCount) {
                val child = parent.getChildAt(i)
                val pos = parent.getChildAdapterPosition(child)
                val isLeft = pos % 2 == 0
                if (isLeft) {
                    val left = child.left.toFloat()
                    val right = left + tagWidth
                    val top = child.top.toFloat()
                    val bottom = child.bottom.toFloat()
                    c.drawRect(left, top, right, bottom, leftPaint)
                } else {
                    val right = child.right.toFloat()
                    val left = right - tagWidth
                    val top = child.top.toFloat()
                    val bottom = child.bottom.toFloat()
                    c.drawRect(left, top, right, bottom, rightPaint)
                }
            }
        }

        init {
            leftPaint = Paint()
            leftPaint.color = context.resources.getColor(R.color.purple_500)
            rightPaint = Paint()
            rightPaint.color = context.resources.getColor(R.color.teal_700)
            tagWidth = context.resources.getDimensionPixelSize(R.dimen.tag_width)
        }
    }

    private class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var title: TextView

        init {
            title = itemView.findViewById(R.id.main_fragment_item_title)
        }
    }

    companion object {
        fun newInstance(): SectionFragment {
            return SectionFragment()
        }
    }
}